<template>
  <div class="login-container">

    <div class="login-bg-box">
      <img src="../../assets/common_images/logo.png" class="login-bg-pic"/>
    </div>

    <div class="login-content-box">
      <div class="login-content-form">
        <el-form ref="loginForm" :model="loginForm" :rules="loginRules" class="login-form" auto-complete="on"
                 label-position="left">

          <div class="title-container">
            <span class="title">后台管理系统</span>
          </div>

          <div class="login-form-item-box">
            <el-form-item prop="username" class="login-form-item">
        <span class="svg-container">
          <svg-icon icon-class="user"/>
        </span>
              <el-input
                ref="username"
                v-model="loginForm.username"
                placeholder="请输入用户名"
                name="username"
                type="text"
                tabindex="1"
              />
            </el-form-item>

            <el-form-item prop="password" class="login-form-item">
        <span class="svg-container">
          <svg-icon icon-class="password"/>
        </span>
              <el-input
                :key="passwordType"
                ref="password"
                v-model="loginForm.password"
                :type="passwordType"
                placeholder="请输入密码"
                name="password"
                tabindex="2"
                @keyup.enter.native="handleLogin"
              />
              <span class="show-pwd" @click="showPwd">
          <svg-icon :icon-class="passwordType === 'password' ? 'eye' : 'eye-open'"/>
        </span>
            </el-form-item>

            <div class="login-form-check">
              <el-checkbox v-model="checked">记住密码</el-checkbox>
            </div>

            <el-button :loading="loading" type="primary" class="login-btn-box" @click.native.prevent="handleLogin">登录
            </el-button>
          </div>
        </el-form>
        <div class="copy-right-box">Copyright © 2018-2020 广东蓝聘网络科技有限公司 粤ICP备18085259号</div>
      </div>
    </div>

  </div>
</template>

<script>
  import {validUsername} from '@/utils/validate';
  import {Base64} from 'js-base64';

  export default {
    name: 'Login',
    data() {
      const validateUsername = (rule, value, callback) => {
        if (value == '' || value == undefined) {
          callback(new Error('请输入用户名'))
        } else {
          callback()
        }
      };
      const validatePassword = (rule, value, callback) => {
        if (value == '' || value == undefined) {
          callback(new Error('请输入密码'))
        } else if (value.length < 6 || value.length > 16) {
          callback(new Error('密码长度位6-16位'))
        } else if (value.indexOf(" ") >= 0) {
          callback(new Error('密码中不能有空格字符'))
        } else if ((/[\u4e00-\u9fa5]+/).test(value)) {
          callback(new Error('密码不能包含中文'))
        } else {
          callback()
        }
      };
      return {
        loginForm: {
          username: '',
          password: ''
        },
        loginRules: {
          username: [{required: true, trigger: 'blur', validator: validateUsername}],
          password: [{required: true, trigger: 'blur', validator: validatePassword}]
        },
        loading: false,
        passwordType: 'password',
        redirect: undefined,
        checked: true
      }
    },
    watch: {
      $route: {
        handler: function (route) {
          this.redirect = route.query && route.query.redirect;
        },
        immediate: true
      }
    },
    mounted() {
      let username = localStorage.getItem("user_name");
      let pwd = localStorage.getItem("user_auth");
      if (pwd) {
        this.loginForm.username = username;
        this.loginForm.password = Base64.decode(pwd);
      } else {
        this.loginForm.username = username;
        this.loginForm.password = "";
      }
    },
    methods: {
      showPwd() {
        if (this.passwordType === 'password') {
          this.passwordType = '';
        } else {
          this.passwordType = 'password';
        }
        this.$nextTick(() => {
          this.$refs.password.focus();
        })
      },
      handleLogin() {
        this.$refs.loginForm.validate(valid => {
          if (valid) {
            this.loading = true;
            this.$store.dispatch('user/loginByUserName', this.loginForm).then((res) => {
              this.loading = false;
              if (res.code === 0) {
                if (res.data == "null" || res.data == "error") {
                  this.$message.error({message: '用户名或密码错误', duration: 1000});
                } else {
                  this.rememberPwd();
                  this.$router.push({path: '/dashboard'});
                }
              }
            }).catch(() => {
              this.loading = false;
            })
          } else {
            return false;
          }
        });
      },
      rememberPwd() {//记住密码
        let username = this.loginForm.username;
        let password = this.loginForm.password;
        if (this.checked) {
          localStorage.setItem("user_name", username);
          localStorage.setItem("user_auth", Base64.encode(password));
        } else {
          localStorage.removeItem("user_auth");
        }
      }
    }
  }
</script>

<style lang="scss">
  /* 修复input 背景不协调 和光标变色 */
  /* Detail see https://github.com/PanJiaChen/vue-element-admin/pull/927 */

  $bg: #283443;
  $light_gray: #333;
  $cursor: #333;

  @supports (-webkit-mask: none) and (not (cater-color: $cursor)) {
    .login-container .el-input input {
      color: $cursor;
    }
  }

  /* reset element-ui css */
  .login-container {
    .el-input {
      display: inline-block;
      height: 47px;
      width: 85%;

      input {
        background: transparent;
        border: 0px;
        -webkit-appearance: none;
        border-radius: 0px;
        padding: 12px 5px 12px 15px;
        color: $light_gray;
        height: 47px;
        caret-color: $cursor;

        &:-webkit-autofill {
          box-shadow: 0 0 0px 1000px #E5E5E5 inset !important;
          -webkit-text-fill-color: $cursor !important;
        }
      }
    }

    .el-form-item {
      border: 1px solid rgba(255, 255, 255, 0.1);
      background: rgba(0, 0, 0, 0.1);
      border-radius: 5px;
      color: #454545;
    }
  }
</style>

<style lang="scss" scoped>
  $bg: #e6f2fa;
  $dark_gray: #3CAFFF;
  $light_gray: #3CAFFF;

  .login-container {
    min-height: 100%;
    width: 100%;
    background-color: $bg;
    display: flex;
    flex-direction: column;
    position: relative;

    .login-bg-box {
      display: flex;
      justify-content: center;
      width: 100%;
      height: 400px;
      background: url("./banner.png") no-repeat;
      background-size: 100% 400px;
      .login-bg-pic {
        margin-top: 120px;
        width: 488px;
        height: 78px;
      }
    }
    .login-content-box {
      width: 100%;
      height: auto;
      .login-content-form {
        display: flex;
        flex-direction: column;
        align-items: center;
        background-color: $bg;
        .login-form {
          position: absolute;
          top: 300px;
          left: 0px;
          right: 0px;
          margin: auto;
          background: #fff;
          width: 426px;
          max-width: 100%;
          overflow: hidden;
          border-radius: 6px;
          .title-container {
            display: flex;
            justify-content: center;
            position: relative;
            .title {
              font-size: 28px;
              padding-top: 40px;
              padding-bottom: 32px;
              color: $light_gray;
              text-align: center;
              font-weight: bold;
            }
          }

          .login-form-item-box {
            display: flex;
            flex-direction: column;
            align-items: center;
            .login-form-item {
              width: 330px;
              height: 48px;
            }
            .login-form-check {
              display: flex;
              align-items: center;
              width: 330px;
              margin-bottom: 18px;
              justify-content: flex-end;
            }
            .login-btn-box {
              width: 330px;
              margin-bottom: 40px;
            }
          }
        }
        .copy-right-box {
          margin-top: 300px;
          width: 100%;
          text-align: center;
          font-size: 14px;
          color: #A4AAB3;
        }
      }
    }

    .svg-container {
      padding: 6px 5px 6px 15px;
      color: $dark_gray;
      vertical-align: middle;
      width: 30px;
      display: inline-block;
    }

    .show-pwd {
      position: absolute;
      right: 10px;
      top: 7px;
      font-size: 16px;
      color: $dark_gray;
      cursor: pointer;
      user-select: none;
    }
  }
</style>
